package com.example.maybeaccount.Mode;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.maybeaccount.AboutActivity;
import com.example.maybeaccount.ActivitySetting;
import com.example.maybeaccount.Entry.NineGridUnlock;
import com.example.maybeaccount.MainActivity;
import com.example.maybeaccount.R;
import com.example.maybeaccount.ResetActivity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * 设置适配器，用于RecyclerView显示
 */
public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.ViewHolder>{
    private ArrayList<String> settingList = new ArrayList<>();
    private Context context;
    private BackupTask backupTask;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView settingItemText;
        public ViewHolder(View view) {
            super(view);
            itemView = view;
            settingItemText = view.findViewById(R.id.setting_name);
        }
    }

    /**
     * 此处设置点击事件
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.frame_setting, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                String settingName = settingList.get(position);
                Intent intent;
                switch (settingName){
                    case "用户名":
                        intent = new Intent(context, ResetActivity.class);
                        intent.putExtra("mode",ResetActivity.MODE_NAME);
                        context.startActivity(intent);
                        break;
                    case "文本密码":
                        intent = new Intent(context, ResetActivity.class);
                        intent.putExtra("mode",ResetActivity.MODE_PSWD);
                        context.startActivity(intent);
                        break;
                    case "图形密码":
                        intent = new Intent(context, NineGridUnlock.class);
                        intent.putExtra("setting",1);
                        context.startActivity(intent);
                        break;
                    case "默认登录方式":
                        ((ActivitySetting)context).showSingleAlertDialog();
                        break;
                    case "数据备份":
                        backupTask = new BackupTask(context);
                        if(backupTask.doInBackground(BackupTask.COMMAND_BACKUP) == null){
                            Toast.makeText(context,"备份失败",Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(context,"备份成功",Toast.LENGTH_LONG).show();
                        }
                        break;
                    case "数据还原":
                        backupTask = new BackupTask(context);
                        if(backupTask.doInBackground(BackupTask.COMMAND_RESTORE) == null){
                            Toast.makeText(context,"还原失败",Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(context,"还原成功",Toast.LENGTH_LONG).show();
                            intent = new Intent(context, MainActivity.class);
                            context.startActivity(intent);
                        }
                        break;
                    case "关于我们":
                        intent = new Intent(context, AboutActivity.class);
                        context.startActivity(intent);
                    default:
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String settingName = settingList.get(position);
        holder.settingItemText.setText(settingName);
    }

    @Override
    public int getItemCount() {
        return settingList.size();
    }

    public SettingAdapter(Context context) {
        this.context = context;
        this.settingList.add("用户名");
        this.settingList.add("文本密码");
        this.settingList.add("图形密码");
        this.settingList.add("默认登录方式");
        this.settingList.add("数据备份");
        this.settingList.add("数据还原");
        this.settingList.add("关于我们");
    }


}
